/*
 * u_slice.c
 *
 *  Created on: 2012-10-6
 *      Author: lin
 */


#include "zmalloc/zmalloc.h"
#include "u_slice.h"

u_slice_t* slice_new0(void) {
    u_slice_t * s = zmalloc(sizeof(u_slice_t));
    s->data = "";
    s->size = 0;
    return s;
}
u_slice_t* slice_new1(const char* d, size_t size) {
    assert(d);
    assert(size <= strlen(d));
    u_slice_t * s = zmalloc(sizeof(u_slice_t));
    s->data = d;
    s->size = size;
    return s;
}

u_slice_t* slice_new2(const char* d) {
    assert(d);
    int size = strlen(d);
    return slice_new1(d, size);
}

void slice_free(u_slice_t *slice) {
    zfree(slice);
}
int slice_compare(const u_slice_t* l, const u_slice_t* r) {
    const size_t lsize = l->size;
    const size_t rsize = r->size;
    const size_t min_len = (lsize < rsize) ? lsize : rsize;
    int cmp = memcmp(l->data, r->data, min_len);
    if (cmp == 0) {
        return lsize < rsize ? -1 : 1;
        if (lsize < rsize)
            cmp = -1;
        else if (lsize > rsize)
            cmp = +1;
    }
    return cmp;
}
